<?php

use Illuminate\Database\Seeder;
use App\Models\Coupon;

class ProductSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $coupons = Coupon::all()->pluck('id')->toArray();
        factory(\App\Models\Product::class, 50)->create()
            ->each(function (\App\Models\Product $product) use ($coupons) {
                // 标签
                $this->tagsWithProduct($product);

                // 封面
                $this->coverWithProduct($product);

                // 图集
                $this->imagesWithProduct($product);

                // SKU
                $this->skuWithProduct($product);

                // 优惠券
                $product->coupons()->sync(randArray($coupons), rand(1, 10));
            });
    }

    protected function tagsWithProduct(\App\Models\Product $product)
    {
        $tags = \App\Models\Tag::all()->pluck('id');
        $product->tags()->sync(randArray($tags->toArray(), rand(1, 5)));
    }

    protected function coverWithProduct(\App\Models\Product $product)
    {
        factory(\App\Models\Image::class)->create([
            'type_id' => $product->id,
            'type' => \App\Models\Image::PRODUCT_COVER,
        ]);
    }

    protected function imagesWithProduct(\App\Models\Product $product)
    {
        factory(\App\Models\Image::class, 2)->create([
            'type_id' => $product->id,
            'type' => \App\Models\Image::PRODUCT_IMAGES,
        ]);
    }

    protected function skuWithProduct(\App\Models\Product $product)
    {
        $attributes = \App\Models\Attribute::all()
            ->pluck('name', 'id')->toArray();

        $meta = [
            '颜色' => [
                '白色',
                '黑色',
            ],
            '尺寸' => [
                'S',
                'L',
            ],
        ];
        $product->skus()->delete();
        foreach ($attributes as $k => $attribute) {
            $attrs = $meta[$attribute] ?? null;

            if ($attrs) {
                $skus = [];
                foreach ($attrs as $attr) {
                    $tmp = [
                        'product_id' => $product->id,
                        'attribute_id' => $k,
                        'attribute_value' => $attr,
                    ];

                    if (!\App\Models\ProductSkuAttribute::query()->where($tmp)->exists()) {
                        $skus[] = $tmp;
                    }
                }
                $product->skus()->createMany($skus);
            }
        }
        $product->sku()->delete();

        $meta_data = [];
        foreach ($product->skus as $skus) {
            $meta_data[$skus->attribute_id][] = $skus->id;
        }
        sort($meta_data);
        foreach ($meta_data[0] as $v) {
            foreach ($meta_data[1] as $vv) {
                $sku = factory(\App\Models\ProductSku::class)->create([
                    'product_id' => $product->id,
                ]);
                factory(\App\Models\Image::class)->create([
                    'type_id' => $sku->id,
                    'type' => \App\Models\Image::SKU_COVER,
                ]);
                $sku->attr()->sync([$v, $vv]);
            }
        }
    }
}
